package cn.tsinghua.comsmacitccntsinghua.httpservice;

import com.google.gson.Gson;
import java.net.URLEncoder;
import java.util.Map;

import cn.tsinghua.comsmacitccntsinghua.bean.NormalResponse;
import cn.tsinghua.comsmacitccntsinghua.tsinterface.IHttpHelper;
import cn.tsinghua.comsmacitccntsinghua.tsinterface.INpResponse;
import cn.tsinghua.comsmacitccntsinghua.tsinterface.IResponse;
import cn.tsinghua.comsmacitccntsinghua.tsinterface.IStringResponse;
import cn.tsinghua.comsmacitccntsinghua.utils.ExceptionUtil;
import cn.tsinghua.comsmacitccntsinghua.utils.StringUtil;


public class HttpHelperImpl implements IHttpHelper {

    @Override
    public void get(String url, IResponse response) {
        try {
            TsHttp.getInstance().get(url,response);
        }catch (Exception e){

        }
    }

    /**
     * statusCode
     * -2
     * -1
     * 0
     * 200
     * 400
     * 500
     * @param url
     * @param map
     * @param response
     */

    @Override
    public void get(String url, Map<String, Object> map, IStringResponse response) {
        try{
            String params="?";
            if(null!=map && map.size()>0){
                for(String key : map.keySet()){
                    Object obj=map.get(key);
                    if(null==obj)obj="";
                    String value=obj.toString();
                    value= URLEncoder.encode(value,"utf-8");
                    params=params+key+"="+value+"&";
                }
                if(params.endsWith("&"))params=params.substring(0,params.length()-1);
            }
            if("?".equals(params))params="";
            url=url+params;
            TsHttp.getInstance().get(url,response);
        }catch (Exception e){
            response.onFail(-2);
        }
    }

    @Override
    public void post(String url, Object obj, IStringResponse response) {
       try{
           String json="";
           if(null!=obj) json=new Gson().toJson(obj);
           TsHttp.getInstance().post(url,json,response);
       }catch (Exception e){
           response.onFail(-2);
       }
    }

    @Override
    public void getnp(String url, Map<String, Object> map,final INpResponse response) {
       this.get(url, map, new IStringResponse() {
           @Override
           public void onSuccess(String result) {
                if(StringUtil.isNullOrEmpty(response)){
                    response.onSuccess(new NormalResponse(false,""));
                }else{
                    try{
                        NormalResponse np=new Gson().fromJson(result,NormalResponse.class);
                        response.onSuccess(np);
                    }catch (Exception e){
                        response.onSuccess(new NormalResponse(false,"返回格式非NormalResponse","",result));
                    }
                }
           }

           @Override
           public void onFail(int statusCode) {
               response.onFail(statusCode);
           }
       });
    }

    @Override
    public void postnp(String url, Object obj,final INpResponse response) {
        this.post(url, obj, new IStringResponse() {
            @Override
            public void onSuccess(String result) {
                if(StringUtil.isNullOrEmpty(response)){
                    response.onSuccess(new NormalResponse(false,""));
                }else{
                    try{
                        NormalResponse np=new Gson().fromJson(result,NormalResponse.class);
                        response.onSuccess(np);
                    }catch (Exception e){
                        ExceptionUtil.getStackTraceInfo(e);
                        response.onSuccess(new NormalResponse(false,"返回格式非NormalResponse","",result));
                    }
                }
            }

            @Override
            public void onFail(int statusCode) {
                response.onFail(statusCode);
            }
        });
    }
}
